package com.zy.mc_module.wheel.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.zy.mc_module.R;
import com.zy.mc_module.utils.SpannableStringUtil;

/**
 * Created on 18/12/14
 * 公用提示对话框
 */
public class HintDialog extends Dialog implements View.OnClickListener {
    private TextView contentTxt;
    private TextView contentTxt2;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;

    private Context mContext;
    private String content;
    private Spanned spanned;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    private View lineView;
    private int btnColor;

    private boolean isShowP, isShowN, isShowT, isDis, isNegativeButtonLight;

    public HintDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public HintDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public HintDialog(Context context, String content) {
        super(context, R.style.BaseDialog);
        this.mContext = context;
        this.content = content;
    }

    public HintDialog(Context context, String content, OnCloseListener listener) {
        super(context, R.style.BaseDialog);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    public HintDialog(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    public HintDialog(Context context, int themeResId, Spanned spanned, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.spanned = spanned;
        this.listener = listener;
    }

    protected HintDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public HintDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    /***
     * 确认按钮
     */
    public HintDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    /***
     * 确认按钮
     */
    public HintDialog setPositiveButtonColor(int color) {
        this.btnColor = color;
        return this;
    }

    /***
     * 取消按钮
     */
    public HintDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    /***
     * 隐藏确认按钮
     * @return
     */
    public HintDialog goneP() {
        this.isShowP = true;
        return this;
    }

    /***
     * 隐藏取消按钮
     * @return
     */
    public HintDialog goneN() {
        this.isShowN = true;
        return this;
    }

    /***
     * 隐藏提示标题
     * @return
     */
    public HintDialog goneT() {
        this.isShowT = true;
        return this;
    }

    /***
     * 点击确认按钮直接关闭,不做任何处理
     * @return
     */
    public HintDialog disSure() {
        this.isDis = true;
        return this;
    }

    /***
     * 设置取消按钮高亮
     * @return
     */
    public HintDialog setNegativeButtonLight() {
        this.isNegativeButtonLight = true;
        return this;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_hint);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        initView();
    }

    private void initView() {
        lineView = findViewById(R.id.hint_view);
        contentTxt = findViewById(R.id.hint_content);
        contentTxt2 = findViewById(R.id.dialog_hint_content2);
        titleTxt = findViewById(R.id.hint_title);
        submitTxt = findViewById(R.id.hint_sure);
        submitTxt.setOnClickListener(this);
        cancelTxt = findViewById(R.id.hint_cancel);
        cancelTxt.setOnClickListener(this);
        if (null != spanned && spanned.length() > 0) {
            contentTxt.setText(spanned);
        } else {
            //将字符串中的电话号码找出并设置颜色和点击事件
            SpannableStringUtil.setTelUrl(mContext, contentTxt, content);
        }
        if (!TextUtils.isEmpty(positiveName)) {
            submitTxt.setText(positiveName);
        }

        if (btnColor != 0) {
            submitTxt.setTextColor(btnColor);
        }

        if (!TextUtils.isEmpty(negativeName)) {
            cancelTxt.setText(negativeName);
        }

        if (!TextUtils.isEmpty(title)) {
            titleTxt.setText(title);
        }
        //隐藏取消按钮
        if (isShowN) {
            lineView.setVisibility(View.GONE);
            cancelTxt.setVisibility(View.GONE);
        }
        //隐藏确认按钮
        if (isShowP) {
            lineView.setVisibility(View.GONE);
            submitTxt.setVisibility(View.GONE);
        }
        //隐藏标题
        if (isShowT) {
            titleTxt.setVisibility(View.GONE);
        }
        //取消按钮高亮
        if (isNegativeButtonLight) {
            submitTxt.setTextColor(getContext().getResources().getColor(R.color.color_333));
            cancelTxt.setTextColor(getContext().getResources().getColor(R.color.colorPrimaryDark));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hint_cancel:
                if (listener != null) {
                    listener.onClick(this, false);
                }
                this.dismiss();
                break;
            case R.id.hint_sure:
                if (listener != null) {
                    listener.onClick(this, true);
                }
                if (isDis) {
                    this.dismiss();
                }
                break;
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm);
    }
}
